logos基础语法

Logos 语法

  1. %hook className 指定需要hook的class,必须以end结尾 如:hook了系统界面的home按钮按下方法

    1
    2
    3
    4
    5
    6
    %hook SpringBoard
    - (void)_munuButtonDown:(id)down{
    NSLog(@"You are press the home button");
    %orig;//call the original _menuButton
    }
    %end
  2. %log 打印日志

    1
    %log((NSString *)@"iOSRE", (NSString *)@"Debug");
  3. %orig 执行原始代码,必须在%hook里面使用,如果在hook方法里面不增加这一行代码,那么原始函数不会被调用。另外还可以用该函数修改原始函数的参数。

    1
    2
    3
    4
    5
    6
    7
    %hook SBLockScreenDateViewController
    //以下方法设置锁屏界面的日期的文字,改成了lemon love well
    - (void)setCustomSubtitleText:(id)arg1 withColor:(id)arg2
    {
    %orig(@"Lemon love Well",arg2);
    }
    %end
  4. %group 用户将%hook分组,便于代码管理和条件初始化分组,必须以%end结尾,一个%group可以包括多个%hook,如果一个%hook不在一个%group里面,那么这个%hook属于%group_ungrouped。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    %group iOS7Hook
    %hook iOS7Class
    - (id)iOS7Method{
    id result = %orig;
    NSLog(@"This class & method only exist in iOS
    7.");
    return result;
    }
    %end
    %end
    %group iOS8Hook
    %hook iOS8Class
    - (id)iOS8Method{
    id result = %orig;
    NSLog(@"This class & method only exist in iOS
    8.");
    return result;
    }
    %end
    %end
  5. %init 值得注意的是,每个%group必须配合下面的%init使用才能生效,该指令用户初始化一个%group,必须在%hook里面调用或者%ctor里面调用。如果带参数,那么初始化指定的group,如果不带参数,那么初始化_ungrouped。如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    	#ifndef kCFCoreFoundationVersionNumber_iOS_8_0
    #define kCFCoreFoundationVersionNumber_iOS_8_0 1140.10
    #endif
    %hook SpringBoard
    - (void)applicationDidFinishLaunching:(id)application
    {
    %orig;
    %init; // Equals to %init(_ungrouped)
    if (kCFCoreFoundationVersionNumber >=
    kCFCoreFoundationVersionNumber_iOS_7_0 &&
    kCFCoreFoundationVersionNumber <
    kCFCoreFoundationVersionNumber_iOS_8_0) %init(iOS7Hook);//init iOS7Hook group
    if (kCFCoreFoundationVersionNumber >=
    kCFCoreFoundationVersionNumber_iOS_8_0) %init(iOS8Hook);
    }
    %end
  6. %ctor,tweak的constructor,完成初始化工作,如果不显式定义,theos会自动生成一个%ctor,并在里面调用%init(_ungrouped)。如

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //以下代码段可以生效,因为theos隐式调用了%ctor并且%init(_ungroped)
    %hook SpringBoard
    - (void)reboot
    {
    NSLog(@"If rebooting does not word then i screwed");
    %orig;//call origin method
    }
    %end
    //以下代码段不能生效,虽然显式定义了%ctor,但是没有在里面调用%init方法
    %hook SpringBoard
    - (void)reboot
    {
    NSLog(@"If rebooting does not word then i screwed");
    %orig;//call origin method
    }
    %ctor{
    //need to call %init here.
    }
    %end
  1. %new在%hook内部使用,给一个现有的class添加新的函数,功能与class_addMethod一样。

    1
    2
    3
    4
    5
    6
    %hook SpringBoard
    %new
    - (void)createNewMethod
    {
    NSLog(@"This is a new method");
    }
  2. %c 该指令的作用等同于NSClassFromString()或者obj_getClass()动态获取一个类的定义,在%hook或者%ctor内使用。

-------评论系统采用disqus,如果看不到需要翻墙-------------